package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @outhor fufeixiang
 * @date 2021-05-06
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/demo")
    @ResponseBody
    public String demo(){

        return  "框架整合初步完成";
    }
    /**
     * 查询所有的用户列表数据,在userList.html中展现数据
     */
    @RequestMapping("/userList")
    public String userList(Model model){
        //1.查询业务层获取数据
        List<User> userList = userService.findAll();
        //2.将数据保存到Model对象中返回给页面
        model.addAttribute("userList",userList);
        return "userList";
    }
    /*
    * 需求：利用restFul实现用户数据新增
    *       新增之后重定向到userList.html页面
    *
    * */
    @GetMapping("/addUser/{name}/{age}/{sex}")
//    @RequestMapping("/restFul")
//    public String insertUser(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
    public String insertUser(User user){

        userService.insertUser(user);
        return "redirect:/userList";
    }


}
